package com.inspur.his.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.inspur.his.constant.Constant;
import com.inspur.his.pojo.Doctzb;
import com.inspur.his.service.DoctzbService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
@Controller
public class DoctzbController {
    @Autowired
    private DoctzbService doctzbService;

    @RequestMapping("doctzbListController")
    public String doctzbList(HttpServletRequest request, Model model, @RequestParam(value = "pn", defaultValue = "1") Integer pn) {
        //获取页面提交来的查询条件
        String ddid = request.getParameter("ddid");
        String dutyday = request.getParameter("dutyday");
        //  点击医生管理左侧菜单进行默认无条件查询
        HashMap map = new HashMap();
        map.put("ddid", ddid);
        map.put("dutyday",dutyday);
        PageHelper.startPage(pn, 5);
        List<Doctzb> doctzbList = doctzbService.doctzbList(map);
        PageInfo<Doctzb> pageInfo = new PageInfo<Doctzb>(doctzbList, doctzbList.size());
        model.addAttribute("doctzbList", doctzbList);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("ddid", ddid);
        model.addAttribute("dutyday", dutyday);
        return "doct/doctzbList.jsp";
    }


    @RequestMapping("doctzbAddController")
    public String doctzbAdd(Doctzb doctzb, Model model) throws Exception {
        //参数绑定 user对象中已经绑定了表单中的用户信息(确认sql中插入的所有字段均有值吗？）
        //思考主键的生成方式--多种1 MySQL中的主键自增
        Date day = new Date();
        SimpleDateFormat fm = new SimpleDateFormat("yyyyMMdd");
        String today = fm.format(day);
        doctzb.setDdid(today + String.valueOf(day.getTime()));
        doctzb.setDutyday(day);
        doctzbService.doctzbAdd(doctzb);
        model.addAttribute("doctzb", doctzb);
        model.addAttribute("success", "医生信息添加成功！");
        return "doct/doctzbList.jsp";
    }
}
